package com.xuelang.pipeline.client.common.result;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

/**
 * 返回结果bean
 *
 * @author ydw
 * @since 2021/05/08
 **/
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ApiResult<T> implements Serializable {



    /**
     * 成功标志
     */
    private boolean success;
    /**
     * 返回码
     * 200:成功
     * 500:失败
     */
    private int code;
    /**
     * 返回信息
     * 成功：success
     * 失败：失败原因
     */
    private String message;
    /**
     * 返回数据
     * 成功：返回数据
     * 失败：null
     */
    private T data;

    public static ApiResult<Object> success() {
        ApiResult<Object> result = new ApiResult<>();
        result.setSuccess(Boolean.TRUE);
        result.setCode(HttpStatus.OK.value());
        return result;
    }

    public static <T> ApiResult<T> success(T data) {
        ApiResult<T> result = new ApiResult<>();
        result.setSuccess(Boolean.TRUE);
        result.setCode(HttpStatus.OK.value());
        result.setData(data);
        return result;
    }

    public static <T> ApiResult<T> fail(int code, String msg) {
        ApiResult<T> result = new ApiResult<>();
        result.setSuccess(Boolean.FALSE);
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    public static <T> ApiResult<T> fail(String msg) {
        ApiResult<T> result = new ApiResult<>();
        result.setSuccess(Boolean.FALSE);
        result.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        result.setMessage(msg);
        return result;
    }
}